package com.power.using.askapp.view;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ScrollView;

/**
 * If you change her, I will ko you
 * sometimes I believe compiler ignores all my comments
 * Created by gyyak46 on 2017/4/11.
 */

public class JeScroolView extends ScrollView {
    private int mTouchSlop;
    private View inner;
    private int top;
    private float y;
    private boolean isMoveing = false;
    private boolean isCount = false;
    private Rect rect = new Rect();
    public JeScroolView(Context context) {
        super(context);
    }
    public JeScroolView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public JeScroolView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (getChildCount() > 0) {
            inner = getChildAt(0);
        }
    }
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (inner != null) {
            switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    //得到第一次按下的值
                    y = ev.getY();
                    top = 0;
                    break;
                case MotionEvent.ACTION_MOVE://按下的时候再次计算值
                    final float pressY = y;
                    float nowY = ev.getY();
                    int slideY = (int) (nowY - pressY);  //得到移动的距离
                    if (!isCount) {
                        slideY = 0;  //归0
                    }
                    if (Math.abs(slideY) < mTouchSlop && top <= 0) {
                        return true;
                    }
                    isNeedMove(); //判断是否移动控件
                    if (isMoveing) { //移动
                        //初始头布局
                        if (rect.isEmpty()) {
                            //保存正常布局位置
                            rect.set(inner.getLeft(), inner.getTop(), inner.getRight(), inner.getBottom());
                        }
                        inner.layout(inner.getLeft(), inner.getTop() + slideY / 4, inner.getRight(), inner.getBottom() + slideY / 4);
                        top += (slideY / 4);
                    }
                    isCount = true;
                    y = nowY;
                    break;
                case MotionEvent.ACTION_UP:
                    isMoveing = false;
                    //动画 移动布局
                    if (isNeedAnimation()) {
                        animation();
                    }
                    break;
            }
        }
        return super.onTouchEvent(ev);
    }
    private void animation() {
        TranslateAnimation translate = new TranslateAnimation(0, 0, inner.getTop(), rect.top);
        translate.setDuration(1000);
        translate.setInterpolator(new DecelerateInterpolator());
        //OvershootInterpolator
        inner.setAnimation(translate);
        //回到正常位置
        inner.layout(rect.left, rect.top, rect.right, rect.bottom);
        rect.setEmpty(); //置为空
        isCount = false;  //手指松开值0
        y = 0;
    }
    /**
     * 是否需要移动布局
     * inner.getMeasureHeight() 获取的控件的总高度
     * getHeight();得到屏幕的高度
     */
    private void isNeedMove() {
        int offSet = inner.getMeasuredHeight() - getHeight(); //得到的差值
        int scrollY = getScrollY(); //得到滑动的距离
//        Log.i("scrollY", "scrollY" + scrollY);
        if (scrollY == 0 || scrollY == offSet) {
            isMoveing = true;
        }
    }
    public boolean isNeedAnimation() {
        return !rect.isEmpty();
    }

}
